#include "playscene.h"
#include <QDebug>
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include "mycoin.h"
#include "dataconfig.h"

PlayScene::PlayScene(int index)
{
    this->levelIndex=index;
    qDebug()<<"您进入的是第"<<this->levelIndex<<"关";

    //设置固定尺寸
    this->setGeometry(QRect(400,150,320,588));
    //设置标题
    this->setWindowTitle("把所有的金币翻出来吧");
    //设置图标
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //创建菜单栏
    QMenuBar *bar=menuBar();
    this->setMenuBar(bar);
    //创建菜单
    QMenu *startMenu=bar->addMenu("开始");
    //创建菜单项
    QAction *quitAction=startMenu->addAction("退出");
    //监听退出
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });


    //返回按钮
    MyPushButton *backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    connect(backBtn,&MyPushButton::clicked,[=](){
        //延时发送返回按钮信号
         QTimer::singleShot(400,this,[=](){
        emit this->ChooseSceneBack();
        });

    });

    //显示当前关卡号
    QLabel * label=new QLabel;
    label->setParent(this);
    //设置文本
    QString str=QString("Level：%1").arg(this->levelIndex);
    label->setText(str);
    QFont font("华文行楷",20);
    label->setFont(font);
    //设置大小、位置
    label->setGeometry(QRect(30,this->height()-50,this->width(),50));
    //设置鼠标穿透属性
    label->setAttribute(Qt::WA_TransparentForMouseEvents);

    //初始化关卡的二维数组
    dataConfig config;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            this->gameArry[i][j]=config.mData[this->levelIndex][i][j];
        }
    }

    //创建金币的背景图
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            QLabel *label=new QLabel;
            QPixmap pix;
            pix.load(":/res/BoardNode.png");
            label->setGeometry(0,0,pix.width(),pix.height());
            label->setPixmap(pix);
            label->setParent(this);
            label->move(57+i*pix.width(),200+j*pix.height());

            //创建金币
            QString str;
            if(this->gameArry[i][j]==1)
            {
                str=":/res/Coin0001.png";
            }
            else
            {
                str=":/res/Coin0008.png";
            }
            MyCoin *coin=new MyCoin(str);
            coin->setParent(this);
            coin->move(59+i*pix.width(),204+j*pix.height());
            //给硬币状态赋值
            coin->posX=i;
            coin->posY=j;
            coin->flag=gameArry[i][j];

            //调用changeFlag
            connect(coin,&MyCoin::clicked,[=](){
                coin->changeFlag();
            });

        }
    }

}

void PlayScene::paintEvent(QPaintEvent *)
{
    //创建画家对象
    QPainter painter(this);
    //创建QPixmap对象
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    //绘制背景
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
     pix.load(":/res/Title.png");
     //缩放图片
     pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
     //绘制图片
     painter.drawPixmap(10,30,pix);


}
